package com.qcit.common.view
import android.content.Context
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.qcit.common.R
import com.qcit.user.bean.common.topx


class MenuView : RelativeLayout {

    var widthm = 0
    var heightm = 0

    var menu_title: String? = "TITLE"
    var menu_subtitle: String? = "SUB TITLE"
    var titleSize: Int? = 15.topx()
    var titleColor = Color.parseColor("#333333")
    var paint: Paint? = null

    var iv_right_arrow: ImageView? = null
    var iv_icon: ImageView? = null
    var tv_title: TextView? = null
    var tv_subtitle: TextView? = null
    var rightArrowVisible: Boolean = true
    var icon: Drawable? = null

    constructor(mContext: Context, attrs: AttributeSet) : super(
        mContext,
        attrs
    ) {
        val inflate = inflate(context, R.layout.view_menu, this)
        iv_right_arrow = inflate.findViewById<ImageView>(R.id.iv_right_arrow)
        iv_icon = inflate.findViewById<ImageView>(R.id.iv_icon)
        tv_title = inflate.findViewById<TextView>(R.id.tv_title)
        tv_subtitle = inflate.findViewById<TextView>(R.id.tv_subtitle)

        val context = mContext
        val a = mContext.obtainStyledAttributes(attrs, R.styleable.MenuView)
        menu_title = a.getString(R.styleable.MenuView_menutitle)
        icon = a.getDrawable(R.styleable.MenuView_menuicon)
        rightArrowVisible = a.getBoolean(R.styleable.MenuView_rightArrowVisible,rightArrowVisible)
        a.recycle()

        iv_icon?.visibility = View.GONE
        if (icon != null) {
            iv_icon?.visibility = View.VISIBLE
            iv_icon?.setImageDrawable(icon)
        }
        if (!rightArrowVisible) {
            iv_right_arrow?.visibility = View.GONE
        }
        tv_title?.text = menu_title
        tv_title?.setTextColor(titleColor)

    }

    fun setSubTitle(subtitle:String?){
        menu_subtitle=subtitle
        tv_subtitle?.setText(menu_subtitle)
    }
    fun setTitle(title:String?){
        menu_title=title
        tv_title?.setText(title)
    }
//    fun setSubTitle(subtitle:String){
//        subtitle=subtitle
//    }
    constructor(mContext: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        mContext,
        attrs,
        defStyleAttr
    )

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        widthm = measuredWidth
        heightm = measuredHeight
    }
}